/**
 * \file CStr.h
 * \brief String class
 * \author Wouter Caarls <w.caarls@tnw.tudelft.nl> 
 *
 * \verbatim
 * Copyright (c) 2005 Wouter Caarls, Delft University of Technology
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * \endverbatim
 */

#ifndef __CSTR_H_INCLUDED
#define __CSTR_H_INCLUDED

#include <string>
#include <sstream>

class CStr
{
public:
  CStr()
  {
    m_str = NULL;
  }
  ~CStr()
  {
    if (m_str) delete m_str;
  }

  CStr(const CStr& rhs)
  {
    m_str = NULL;
    m_buf << rhs.m_buf.str();
  }

  template <class T>
  CStr(T rhs)
  {
    m_str = NULL;
    m_buf << rhs;
  }

  template <class T>
  CStr& operator<<(const T rhs)
  {
    m_buf << rhs;
    return *this;
  }

  operator char *()
  {
    if (m_str) delete m_str;
    m_str = new std::string(m_buf.str());
    return (char*)m_str->c_str();
  }

  std::string str()
  {
    return m_buf.str();
  }

  void put(char c)
  {
    m_buf.put(c);
  }

private:
  std::ostringstream m_buf;
  std::string *m_str;
};

#endif /* __CSTR_H_INCLUDED */
